package cn.duli.xianer.controller;


import cn.duli.xianer.config.IdcardConfig;
import cn.duli.xianer.pojo.MyResult;
import cn.duli.xianer.pojo.UserAuth;
import cn.duli.xianer.pojo.Users;
import cn.duli.xianer.service.FileService;
import cn.duli.xianer.service.UserAuthService;
import cn.duli.xianer.service.UsersService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping("/userAuth")
public class UserAuthController {

    @Autowired
    private UserAuthService userAuthService;

    @Autowired
    FileService fileService;

    @Autowired
    private UsersService usersService;

    /**
     * 根据用户id检查该用户是否实名认证
     * @param uaUid
     * @return
     */
    @GetMapping("/checkUserAuth")
    @RequiresRoles(logical = Logical.OR, value = {"user", "business", "admin"})
    public MyResult checkUserAuth(Integer uaUid) {
        UserAuth userAuth = userAuthService.findByUserId(uaUid);
        if (userAuth==null) {
            return new MyResult(400, "该用户未实名认证",null,null,null,null);
        } else {
            return new MyResult(200, "该用户已实名认证",null,null,userAuth,null);
        }
    }

    /**
     * 用户新增实名认证（新增前需要判断该用户是否实名认证）
     * @param upFile
     * @return
     * @throws IOException
     */
    @PostMapping(value = "/save")
    @RequiresRoles("user")
    public MyResult save(@RequestParam(value = "file",required = false) MultipartFile upFile, UserAuth userAuth) throws IOException {
        String img_url = fileService.uploadOneFile(upFile);
        // 百度云识别身份证图片
        UserAuth userAuth1 = IdcardConfig.getIdcardInfo(img_url);
        // 将识别出的字段存入数据库表user_auth中
        userAuth.setUaFullname(userAuth1.getUaFullname());
        userAuth.setUaNation(userAuth1.getUaNation());
        userAuth.setUaAddr(userAuth1.getUaAddr());
        userAuth.setUaIdcard(userAuth1.getUaIdcard());
        userAuth.setUaBirthday(userAuth1.getUaBirthday());
        userAuth.setUaGender(userAuth1.getUaGender());
        userAuth.setUaImg(img_url);
        int uaId = userAuthService.save(userAuth);
        // 修改此用户角色为 business
        Users user = usersService.findById(userAuth.getUaUid());
        user.setuRole("business");
        usersService.updateRole(user);
        UserAuth userInfo = userAuthService.findById(uaId);
        return new MyResult(200, "用户新增实名认证成功",null,null,userInfo,null);
    }

    /**
     * 根据用户id查询该用户认证信息
     * @param uaUid
     * @return
     */
    @GetMapping("/findByUserId/{uaUid}")
    @RequiresRoles(logical = Logical.OR, value = {"user", "admin", "business"})
    public MyResult findByUserId(@PathVariable("uaUid") Integer uaUid) {
        UserAuth userAuth = userAuthService.findByUserId(uaUid);
        return new MyResult(200, "根据用户id查询该用户认证信息成功",null,null,userAuth,null);
    }


    /**
     * 分页查询所有用户认证信息
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("/findAll")
    @RequiresRoles("admin")
    public MyResult findAll(@RequestParam(defaultValue = "1") int pageNo,
                                   @RequestParam(defaultValue = "6") int pageSize) {
        PageHelper.startPage(pageNo, pageSize);
        PageInfo<UserAuth> pageInfo = new PageInfo<>(userAuthService.findAll());
        return new MyResult(200, "分页查询所有用户认证信息成功",null,null,null,pageInfo);
    }

    /**
     * 修改用户实名认证的支付宝信息
     * @param userAuth
     * @return
     */
    @RequestMapping("/updateAlipay")
    @RequiresRoles("business")
    public MyResult updateAlipay(UserAuth userAuth) {
        userAuthService.updateAlipay(userAuth);
        UserAuth userInfo = userAuthService.findByUserId(userAuth.getUaUid());
        return new MyResult(200, "修改用户实名认证的支付宝信息成功",null,null,userInfo,null);
    }

}
